package com.ctsi.organise.event.listener;

import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.authentication.event.AuthenticationFailureBadCredentialsEvent;
import org.springframework.stereotype.Component;

import java.util.LinkedHashMap;

/**
 * @author zhangjw
 * @className LoginFailureListener
 * @description TODO
 * @date 2019/3/2 14:40
 **/
@Component
public class LoginFailureListener implements ApplicationListener<AuthenticationFailureBadCredentialsEvent> {


    @Async
    @Override
    public void onApplicationEvent(AuthenticationFailureBadCredentialsEvent event) {
        String detailMessage = event.getException().getMessage();
        Object obj = event.getSource();
        if (obj instanceof UsernamePasswordAuthenticationToken) {
            UsernamePasswordAuthenticationToken token = (UsernamePasswordAuthenticationToken) obj;
            LinkedHashMap<String,Object> details = (LinkedHashMap<String, Object>) token.getDetails();
            String id = (String) details.get("requestId");
        }

    }
}
